package com.itcast.catchfiveeight;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

/**
 * Created by Administrator on 2016/2/22 0022.
 */
public class GetPage {

    public static Page get58Data(String url)  {
        //获取到58同城的链接
      //  String url = "http://su.58.com/zptaobao/21166833517577x.shtml";
        String userAgent = "Mozilla/5.0（Linux；U；Android2.2；en-us；NexusOneBuild/FRF91）AppleWebKit/533.1（KHTML，likeGecko）Version/4.0MobileSafari/533";
        Document doc = null; // 设置 User-Agent;设置超时1秒
        try {
            doc = Jsoup.connect(url).userAgent(userAgent).get();
            Element eCompany = doc.select("h2.c_tit.c_tit_ell a").first();//公司名称
            Element eHangye = doc.select("#openCom.dcompany_open div.company_con.btOnepx ul li span.attrValue a[target=_blank]").first();//行业
            Element eGuimo = doc.select("#openCom.dcompany_open div.company_con.btOnepx ul li span.attrValue").first();//规模
            Element eXingzhi = doc.select("#openCom.dcompany_open div.company_con.btOnepx ul li:nth-child(2) span.attrValue").first();//性质
            Element eAddress = doc.select("#openCom.dcompany_open div.company_con.btOnepx ul li span.attrValue.dizhiValue").first();//工作地址
            Element ePhone = doc.select("#contact_phone.fphone").first();
            Element eMan = doc.select("#openCom.dcompany_open div.com div.comWrap div.contact span:nth-child(2)").first();
            Element eCity = doc.select("a.city span.city-text").first();

            boolean isPaiqian = false;
            Element ifDaizhao = doc.select("span.d_titico").first();
            Element daizhao = doc.select("span.hint").first();
            if(null!=ifDaizhao && ifDaizhao.text().equals("代招")){
                isPaiqian = true;
            }
            if(null!=daizhao ){
                if(daizhao.text().equals("代招")){
                    isPaiqian = true;
                }
                if(daizhao.text().equals("派遣")){
                    isPaiqian = true;
                }
                if(daizhao.text().equals("人力资源机构")){
                    isPaiqian = true;
                }
            }
            Page entity = new Page();
            String eStrConpany = "";
            String eStrHangye  = "其他行业";
            String eStrGuimo   = "";
            String eStrXingzhi = "";
            String eStrAddress = "";
            String eStrPhone   = "";
            String eStrMan     = "";
            String eStrCity    = "";
            if(null!=eCompany){
                eStrConpany = eCompany.text();
            }
            if(null!=eHangye){
                eStrHangye = eHangye.text();
            }
            if(null!=eGuimo){
                eStrGuimo   = eGuimo.text();
            }
            if(null!=eXingzhi){
                eStrXingzhi = eXingzhi.text();
            }
            if(null!=eAddress){
                eStrAddress = eAddress.text();
            }
            if(null!=ePhone){
                eStrPhone   = ePhone.attr("phoneno");
            }
            if(null!=eMan){
                eStrMan  = eMan.text();
            }

            if(null!=eCity){
                eStrCity    = eCity.text();
            }
            if(eStrHangye.contains("人力资源服务")){
                isPaiqian = true;
            }

            entity.seteStrCompany(eStrConpany);
            entity.seteStrHangye(eStrHangye);
            entity.seteStrAddress(eStrHangye);
            entity.seteStrGuimo(eStrGuimo);
            entity.seteStrXingzhi(eStrXingzhi);
            entity.seteStrAddress(eStrAddress);
            entity.seteStrPhone(eStrPhone);
            entity.seteStrMan(eStrMan);
            entity.seteStrCity(eStrCity);
            entity.setPaiqian(isPaiqian);
            // System.out.println(entity.toString());
            return entity;
        } catch (Exception e) {
            e.printStackTrace();
            boolean flag = true;
            Page eaa = new Page();
            eaa.setPaiqian(flag);
            return eaa;
        }

    }
}
